/*
*	Copyright (C) 2008-2009  Devioux.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.devioux.inter.str;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import com.devioux.inter.VarChecker;

public class StrParser {
	
	private int count;
	private String str;
	
	public StrParser(String input) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException, NoSuchMethodException {
		
		List<Integer> count = new QuoteHandler(input).getOccurances();
		
		if (count.size() == 0) {
			str = input;
		} else {
			String varContainer, var, type;
			Object output;
						
			for (int i = 0; i < count.size() / 2; i += 2) {
				varContainer  = input.substring(count.get(i), count.get(i + 1) + 1);
				var           = varContainer.substring(4, varContainer.length() - 4);
				type          = new VarChecker(var).check();
				
				Class<?> cls  = Class.forName("com.devioux.inter.lang." + type);
				Object   obj  = cls.newInstance();
				Method   meth = cls.getMethod("getValue", new Class[]{String.class});
				output        = meth.invoke(obj, new Object[]{var});
				
				input         = input.replace(varContainer, output.toString());
			
			}
			
			if (!input.startsWith("\"")) {
				varContainer = input.substring(0, input.indexOf("\"") + 1);
				var          = varContainer.substring(0, varContainer.length() - 4);
				type          = new VarChecker(var).check();
				
				Class<?> cls  = Class.forName("com.devioux.inter.lang." + type);
				Object   obj  = cls.newInstance();
				Method   meth = cls.getMethod("getValue", new Class[]{String.class});
				output        = meth.invoke(obj, new Object[]{var});
				
				input         = input.replace(varContainer, output.toString());
			} else {
				input = input.substring(1);
			}
			
			
			if (!input.endsWith("\"")) {
				varContainer = input.substring(input.lastIndexOf("\""));
				var          = varContainer.substring(4);
				type          = new VarChecker(var).check();
				
				Class<?> cls  = Class.forName("com.devioux.inter.lang." + type);
				Object   obj  = cls.newInstance();
				Method   meth = cls.getMethod("getValue", new Class[]{String.class});
				output        = meth.invoke(obj, new Object[]{var});
				
				input         = input.replace(varContainer, output.toString());
			} else {
				input = input.substring(0, input.length() - 1);
			}
			
			input = input.replace("\\\"", "\"");
			
			str = input;
		}
	}
	
	public int getCount() {
		return count;
	}
	
	public String getString() {
		return str;
	}
}
